package itheima.zuoye.Test1;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static org.slf4j.Logger log2 = LoggerFactory.getLogger(itheima.d07.Test.class);
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {

        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse("day12\\src\\itheima\\zuoye\\Test1\\users.xml");
            NodeList nodeList = document.getElementsByTagName("user");
            Map<Integer, User> UserMap = new HashMap<>();

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node userNode = nodeList.item(i);

                if (userNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element userElement = (Element) userNode;
                    int id = Integer.parseInt(userElement.getAttribute("id"));
                    String name = userElement.getElementsByTagName("name").item(0).getTextContent();
                    log2.info("User name: {}", name);
                    String sex = userElement.getElementsByTagName("sex").item(0).getTextContent() ;

                    String address = userElement.getElementsByTagName("address").item(0).getTextContent();
                    String password = userElement.getElementsByTagName("password").item(0).getTextContent();




                    User user= new User(name, sex, address, password);
                    UserMap.put(id, user);

                }
            }

         UserMap.forEach((k,v)-> System.out.println("ID："+k+"---"+v));



        }catch (Exception e){
            e.printStackTrace();
        }









    }
}
